package br.com.medmark.bean;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.primefaces.util.MessageFactory;

import br.com.medmark.model.hibernate.Administrador;
import br.com.medmark.model.hibernate.Clinica;
import br.com.medmark.model.hibernate.Usuario;
import br.com.medmark.model.hibernate.dao.AdministradorDAO;
import br.com.medmark.model.hibernate.dao.ClinicaDAO;
import br.medmark.servico.AdministradorServico;
import br.medmark.servico.ClinicaServico;
import br.medmark.servico.UsuarioServico;
import br.medmark.util.Constantes;

@Named
@SessionScoped
public class BeanUsuario implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8174611009837452731L;

	protected static Logger			logger	= Logger.getLogger(BeanGenerico.class.getName());

	private String login;
	private String senha;
	private Usuario user;
	
	public BeanUsuario() {
		// TODO Auto-generated constructor stub
	}
	
	public String validaLogin(String login,String senha){
		FacesContext facesContext = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
		UsuarioServico usuarioServico = UsuarioServico.getInstance();
		user = usuarioServico.validaUsuario(login, senha);
		if (user != null) {	
			if (user.getTipo_usuario() == Constantes.CODIGO_CLINICA) {
				Clinica clinica = ClinicaDAO.getInstance().getClinica(login, senha);
				session.setAttribute("clinica", clinica);
				return ClinicaServico.getInstance().exibeTelaInicial();
			}
			else {
				 Administrador adm = AdministradorDAO.getInstance().getAdministrador(login, senha);
				 session.setAttribute("administrador", adm);
				 return AdministradorServico.getInstance().exibeTelaInicial();
			}

		}else{
			
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Verifique o login e a senha e tente novamente!", null));
			//BeanUtil.addMessage(FacesMessage.SEVERITY_WARN, "login_usuario_login_senha_invalido");
		
		}
		return null;		
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getSenha() {
		return senha;
	}

	public void setSenha(String senha) {
		this.senha = senha;
	}

	public Usuario getUser() {
		return user;
	}

	public void setUser(Usuario user) {
		this.user = user;
	}
	
	
}
